package com.centit.framework.ip.businessdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.ip.businessdata.po.InterfaceManager;
import com.centit.framework.ip.businessdata.po.InterfaceMethod;
import com.centit.framework.ip.businessdata.service.InterfaceManagerManager;
import com.centit.framework.ip.businessdata.service.InterfaceMethodManager;
import com.centit.framework.ip.util.layuiResultUtil;
import com.centit.support.algorithm.UuidOpt;
import com.centit.support.database.utils.PageDesc;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * InterfaceMethod  Controller.
 * create by scaffold 2018-05-22
 *
 * @author guo_jh
 * T_INTERFACE_METHOD业务系统方法配置信息表
 */


@Controller
@RequestMapping("/interface/interfacemethod")
public class InterfaceMethodController extends BaseController {
    private static final Log log = LogFactory.getLog(InterfaceMethodController.class);

    @Resource
    private InterfaceMethodManager interfaceMethodMag;
    @Resource
    private InterfaceManagerManager interfaceManagerMag;


    /**
     * 查询所有   T_INTERFACE_METHOD  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(HttpServletRequest request, HttpServletResponse response) {
        //解析参数信息
        Map<String, Object> searchColumn = convertSearchColumn(request);
        //解析分页信息
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 20;
        pageNo = StringUtils.isNotEmpty(page) ? Integer.valueOf(page) : pageNo;
        pageSize = StringUtils.isNotEmpty(limit) ? Integer.valueOf(limit) : pageSize;
        PageDesc pageDesc = new PageDesc(pageNo, pageSize);

        JSONArray jsonArrayObjects = interfaceMethodMag.listObjectsAsJson(searchColumn, pageDesc);

        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);
        layuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);
    }

    /**
     * 查询单个  T_INTERFACE_METHOD
     *
     * @param ywid     YWID
     * @param response {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{ywid}", method = {RequestMethod.GET})
    public void getInterfaceMethod(@PathVariable String ywid, HttpServletResponse response) {

        InterfaceMethod interfaceMethod =
                interfaceMethodMag.getObjectById(ywid);
        InterfaceManager interfaceManager = interfaceManagerMag.getObjectById(interfaceMethod.getRecordid());
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData("interfaceMethod", interfaceMethod);
        resData.addResponseData("systemname", interfaceManager.getSystemname());
        JsonResultUtils.writeResponseDataAsJson(resData, response);
    }

    /**
     * 新增 T_INTERFACE_METHOD，业务只有新增的时候可用此方法，如有修改可用下面 新增或保存合并的方法
     *
     * @param interfaceMethod {@link InterfaceMethod}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public void createInterfaceMethod(@Valid InterfaceMethod interfaceMethod, HttpServletRequest request, HttpServletResponse response) {
        //判断接口标识是否已存在
        InterfaceMethod method = this.interfaceMethodMag.getObjectByProperty("methodflag", interfaceMethod.getMethodflag());
        if (null != method) {
            JsonResultUtils.writeErrorMessageJson("方法标识已存在", response);
        } else {
            //填充系统接口信息
            interfaceMethod.setYwid(UuidOpt.getUuidAsString());
            interfaceMethod.setStartOrStop("T");
            interfaceMethod.setCreater(getLoginUserCode(request));
            interfaceMethod.setCreateTime(new Date());
            interfaceMethodMag.saveNewObject(interfaceMethod);
            JsonResultUtils.writeSingleDataJson("操作成功", response);
        }
    }

    /**
     * 删除单个  T_INTERFACE_METHOD
     *
     * @param ywid YWID
     */
    @RequestMapping(value = "/{ywid}", method = {RequestMethod.DELETE})
    public void deleteInterfaceMethod(@PathVariable String ywid, HttpServletRequest request, HttpServletResponse response) {
        //逻辑删除
        /*InterfaceMethod dbInterfaceMethod =
                interfaceMethodMag.getObjectById(ywid);
        dbInterfaceMethod.setUpdater(getLoginUserCode(request));
        dbInterfaceMethod.setUpdateTime(new Date());
        dbInterfaceMethod.setStartOrStop("F");
        interfaceMethodMag.mergeObject(dbInterfaceMethod);*/
        interfaceMethodMag.deleteObjectById(ywid);
        JsonResultUtils.writeSingleDataJson("操作成功", response);
    }

    /**
     * 新增或保存 T_INTERFACE_METHOD
     *
     * @param ywid            YWID
     * @param interfaceMethod {@link InterfaceMethod}
     * @param response        {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{ywid}", method = {RequestMethod.PUT})
    public void updateInterfaceMethod(@PathVariable String ywid,
                                      @Valid InterfaceMethod interfaceMethod, HttpServletRequest request, HttpServletResponse response) {


        InterfaceMethod dbInterfaceMethod =
                interfaceMethodMag.getObjectById(ywid);


        if (null != dbInterfaceMethod) {//如果数据库数据存在，则是更新操作
            dbInterfaceMethod.copyNotNullProperty(interfaceMethod);//赋值页面不为空的值，小心页面空的字符串或者改写copyNotNullProperty方法，根据自己业务需要
            //记录更新人及时间
            dbInterfaceMethod.setUpdater(getLoginUserCode(request));
            dbInterfaceMethod.setUpdateTime(new Date());
            interfaceMethodMag.mergeObject(dbInterfaceMethod);
        } else {
            interfaceMethodMag.saveNewObject(interfaceMethod);//新增,注意主键赋值 UuidUtil.getTimeBasedUuid()，填写自己的业务时间，业务操作人等信息
        }

        JsonResultUtils.writeSingleDataJson("操作成功", response);
    }
}
